<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/8
 * Time: 10:37
 */

namespace app\admin\controller;


use app\admin\service\ProductService;
use app\lib\exception\MysqlErrorException;
use Page\Page;
use think\App;
use think\facade\Request;
use app\admin\model\Category;
use app\admin\model\Product as ProductModel;
use app\admin\model\Grade as GradeModel;

class Product extends BaseController
{
    private $categoryName;
    private $gradeName;
    /**
     *  渲染全部商品的页面
     * @return mixed
     */
    public function productList()
    {
        $this->assign([
            'categoryName'=> Category::getAllCategoryName(),
            'gradeName' => GradeModel::getAllGradeName()
        ]);

        return $this->fetch('list/product');
    }

    public function productListStatus()
    {
        $this->assign([
            'categoryName'=> Category::getAllCategoryName(),
            'gradeName' => GradeModel::getAllGradeName()
        ]);

        return $this->fetch('list/productStatus');
    }

    /**
     * 渲染添加商品页面
     * @return mixed
     */
    public function addProduct($id = '')
    {
        if($id){
            $info = ProductModel::findOne($id);
            $this->assign('info',$info);
        }else{
            $this->assign('info',config('admin.product_null'));
        }
        $service = new ProductService();
        $grade_info = $service->grade_info();
        $this->assign([
            'categoryName'=> Category::getAllCategoryName(),
            'gradeName' => GradeModel::getAllGradeName(),
            'info_info' => $grade_info
        ]);

        return $this->fetch('list/addProduct');
    }

    /**
     * layui后台所有商品分类的接口
     * @param $page 第几页
     * @param $limit 每一页的数量
     * @return \think\response\Json
     */
    public function getAllProductByLayui($page,$limit)
    {
        $model = new ProductModel();
        $page = new Page($model,$page,$limit,[],['px'=> 'asc','status'=>'desc']);
        $result = $page->layuiPage();
        return json($result);
    }
    public function getAllProductStatusByLayui($page,$limit)
    {
        $model = new ProductModel();
        $where = [['status', '=', 1]];
        $page = new Page($model,$page,$limit,[],['px'=> 'asc','status'=>'desc'], $where);
        $result = $page->layuiPageStatus();
        return json($result);
    }

    /**
     * 后台添加商品from表单提交的数据
     * @return \think\response\Json|void
     */
    public function add()
    {
        $post = Request::post();
        $service = new ProductService();
        //return json($post);
        if($post['product_id'] == '') {
            $result = $service->addProduct($post);
            if(!$result){
                return $this->success('添加失败','/admin/Product/productList');
            }
            return $this->success('添加成功','/admin/Product/productList');
        }else{
            $result = $service->upProduct($post);
            if(!$result){
                return $this->success('更新失败','/admin/Product/productList');
            }
            return $this->success('更新成功','/admin/Product/productList');
        }
    }

    /** 按条件查询接口
     * @return \think\response\Json
     */
    public function ConditionByProduct()
    {
        $post = $this->request->post();
        //return json($post);
        $service = new ProductService();
        $result = $service->FindConditionByProduct($post);
        return json($result);
    }

    /**
     * 删除一条商品记录
     * @return \think\response\Json
     * @throws MysqlErrorException
     */
    public function del()
    {
        $id = Request::param('id');
        $resulr = ProductModel::delOne($id);
        if (!$resulr){
            throw new MysqlErrorException([
                'code' => 300,
                'msg' => '删除失败'
            ]);
        }
        return json([
            'code' => 200
        ]);
    }
}